﻿namespace Ocelot.Admin.Api.Application
{
    public class Mapper : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<GlobalConfiguration, GlobalConfigurationDto>()
                .Map(dest => dest.RateLimitOptions, src =>
                   !string.IsNullOrWhiteSpace(src.RatelimitClientidheader) ||
                   src.RatelimitDisableratelimitheaders.HasValue ||
                   src.RatelimitHttpstatuscode.HasValue ||
                   !string.IsNullOrWhiteSpace(src.RatelimitQuotaexceededmessage) ||
                   !string.IsNullOrWhiteSpace(src.RatelimitRatelimitcounterprefix) ? new GlobalRateLimitOptionDto()
                   {
                       ClientIdHeader = src.RatelimitClientidheader,
                       DisableRateLimitHeaders = src.RatelimitDisableratelimitheaders,
                       HttpStatusCode = src.RatelimitHttpstatuscode,
                       QuotaExceededMessage = src.RatelimitQuotaexceededmessage
                   } : null)
                .Map(dest => dest.ServiceDiscoveryProvider, src => new ServiceDiscoveryProviderDto() { Host = src.ServicediscoveryHost, Port = src.ServicediscoveryPort ?? 8500, Type = src.ServicediscoveryType, PollingInterval = src.ServicediscoveryPollinginterval ?? 1000 })
                ;

            config.ForType<RouteDto, ConfigRouteDto>()
                .Map(dest => dest.RequestIdKey, src => string.IsNullOrWhiteSpace(src.RequestIdKey) ? null : src.RequestIdKey)
                .Map(dest => dest.ServiceName, src => string.IsNullOrWhiteSpace(src.ServiceName) ? null : src.ServiceName)
                .Map(dest => dest.RateLimitOptions,
              src => src.RatelimitEnableratelimiting.HasValue && src.RatelimitEnableratelimiting.Value == true ? new RateLimitOptionDto()
              {
                  ClientWhitelist = src.RatelimitWhitelist,
                  EnableRateLimiting = true,
                  Limit = src.RatelimitLimit,
                  Period = src.RatelimitPeriod.ToLower(),
                  PeriodTimespan = src.RatelimitPeriodtimespan,
              } : null)
                 .Map(dest => dest.HttpHandlerOptions,
                 src =>
                 src.HttphandlerAllowautoredirect.HasValue ||
                 src.HttphandlerMaxconnectionsperserver.HasValue ||
                 src.HttphandlerUsecookiecontainer.HasValue ||
                 src.HttphandlerUsetracing.HasValue ||
                 src.HttphandlerUseproxy.HasValue ? new HttpHandlerOptionDto()
                 {
                     AllowAutoRedirect = src.HttphandlerAllowautoredirect,
                     MaxConnectionsPerServer = src.HttphandlerMaxconnectionsperserver,
                     UseCookieContainer = src.HttphandlerUsecookiecontainer,
                     UseTracing = src.HttphandlerUsetracing,
                     UseProxy = src.HttphandlerUseproxy
                 } : null)
                 .Map(dest => dest.AuthenticationOptions,
                 src => !string.IsNullOrWhiteSpace(src.AuthenticationAuthenticationproviderkey) ? new AuthenticationOptionDto()
                 {
                     AuthenticationProviderKey = src.AuthenticationAuthenticationproviderkey,
                     AllowedScopes = src.AuthenticationAllowedscopes
                 } : null)
                 .Map(dest => dest.DownstreamHostAndPorts,
                 src => src.RouteHostPorts.Count > 0 ? src.RouteHostPorts.Select(o => new DownstreamHostAndPortDto()
                 {
                     Host = o.Host,
                     Port = o.Port
                 }) : null)
                 //
                 .Map(dest => dest.QoSOptions,
                 src =>
                 src.QosDurationofbreak.HasValue ||
                 src.QosExceptionsallowedbeforebreaking.HasValue ||
                 src.QosTimeoutvalue.HasValue ? new QoSOptionDto()
                 {
                     DurationOfBreak = src.QosDurationofbreak,
                     ExceptionsAllowedBeforeBreaking = src.QosExceptionsallowedbeforebreaking,
                     TimeoutValue = src.QosTimeoutvalue
                 } : null)
                 //设置负载均衡
                 .Map(dest => dest.LoadBalancerOptions, src =>
                    !string.IsNullOrWhiteSpace(src.LoadbalancerType) ? new LoadBalancerOptionDto()
                    {
                        Type = src.LoadbalancerType,
                        Key = src.LoadbalancerKey,
                        Expiry = src.LoadbalancerExpiry
                    } : null)
                 //设置缓存
                 .Map(dest => dest.FileCacheOptions, src =>
                 !string.IsNullOrWhiteSpace(src.CacheRegion) && src.CacheTtlseconds.HasValue ? new FileCacheOptionDto()
                 {
                     Region = src.CacheRegion,
                     TtlSeconds = src.CacheTtlseconds,
                 } : null)
                 .Map(dest => dest.DelegatingHandlers, src => src.DelegatingHandlers == null || src.DelegatingHandlers.Count == 0 ? null : src.DelegatingHandlers);
        }
    }
}